Project 3

1) Taking the libraries

We introduced the libraries “glcm” and “raster in our Markdown in order to get images with raster function and monitoring them with”glcm" function.

library(jpeg)
library(glcm)
## Warning: package 'glcm' was built under R version 3.6.2
library(rgdal)
## Warning: package 'rgdal' was built under R version 3.6.2
## Loading required package: sp
## Warning: package 'sp' was built under R version 3.6.2
## rgdal: version: 1.4-8, (SVN revision 845)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
##  Path to GDAL shared files: C:/Users/user/Documents/R/win-library/3.6/rgdal/gdal
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: C:/Users/user/Documents/R/win-library/3.6/rgdal/proj
##  Linking to sp version: 1.3-2
library(RTextureMetrics)
library(raster)
## Warning: package 'raster' was built under R version 3.6.2

2) Reading grey photos and determining the significance level

Firstly, we turned the images into greyscale format to use our GLCM function. Then, we read the all 20 images with raster function and jpeg fuction.

photo_11 <-raster("C:/Users/user/Desktop/Images/Fabric11g.jpg")
photo_11_jpg <-readJPEG("C:/Users/user/Desktop/Images/Fabric11g.jpg")

photo_12 <-raster("C:/Users/user/Desktop/Images/Fabric12g.jpg")
photo_12_jpg <-readJPEG("C:/Users/user/Desktop/Images/Fabric12g.jpg")

photo_13 <-raster("C:/Users/user/Desktop/Images/Fabric13g.jpg")
photo_13_jpg <-readJPEG("C:/Users/user/Desktop/Images/Fabric13g.jpg")

photo_14 <-raster("C:/Users/user/Desktop/Images/Fabric14g.jpg")
photo_14_jpg <-readJPEG("C:/Users/user/Desktop/Images/Fabric14g.jpg")

photo_15 <-raster("C:/Users/user/Desktop/Images/Fabric15g.jpg")
photo_15_jpg <-readJPEG("C:/Users/user/Desktop/Images/Fabric15g.jpg")

photo_16 <-raster("C:/Users/user/Desktop/Images/Fabric16g.jpg")
photo_16_jpg <-readJPEG("C:/Users/user/Desktop/Images/Fabric16g.jpg")

photo_17 <-raster("C:/Users/user/Desktop/Images/Fabric17g.jpg")
photo_17_jpg <-readJPEG("C:/Users/user/Desktop/Images/Fabric17g.jpg")

photo_18 <-raster("C:/Users/user/Desktop/Images/Fabric18g.jpg")
photo_18_jpg <-readJPEG("C:/Users/user/Desktop/Images/Fabric18g.jpg")

photo_19 <-raster("C:/Users/user/Desktop/Images/Fabric19g.jpg")
photo_19_jpg <-readJPEG("C:/Users/user/Desktop/Images/Fabric19g.jpg")

photo_20 <-raster("C:/Users/user/Desktop/Images/Fabric20g.jpg")
photo_20_jpg <-readJPEG("C:/Users/user/Desktop/Images/Fabric20g.jpg")

3) Creating a method to monitor the defects

We wrote a function which detects the outliers by checking their GLCM attributes. Initially, we found their GLCM statistics.By doing this, we selected window size as 11x11 because too small window size make the data much bigger and it looks like monitoring pixel by pixel. On the other hand, larger window sizes cause data loss. We can’t see outliers clearly.

Additionally, we selected our grey scale level as 32.If all 256 x 256 cells were used, there would be many cells filled with 0’s (because that combination of grey levels simply does not occur on the image). The GLCM approximates the joint probability distribution of two pixels. Having many 0’s in cells makes this a very bad approximation.

Afterwards, we calculated control limits for each statistics and checked pixel values whether in control or out of control. If a pixe’s statistics is out of control, then its ooc condition will be set to 1. Finally, we wrote a characteristic condition which determines the pixel is whether really out of control or not. If they are out of control, then we marked with black. We plotted the images without defects and with defects.

detect_out_of_control <- function(photo, photo_jpg, significance_level){
  
  photo_jpg_defects = photo_jpg
  
  result1 =glcm(photo, n_grey=32 ,
                window = c(11,11), 
                shift=list(c(0,1), c(1,1), c(1,0), c(1,-1)), 
                statistics = c("mean", "variance", "correlation", "homogeneity", "contrast","dissimilarity")
  )
  
  m=as.matrix(result1$glcm_mean)
  h= as.matrix(result1$glcm_homogeneity)
  d = as.matrix(result1$glcm_dissimilarity)
  v = as.matrix(result1$glcm_variance)
  c= as.matrix(result1$glcm_contrast)
  cr = as.matrix(result1$glcm_correlation)
  mean1=mean(m,na.rm=TRUE)
  sd1=sd(m,na.rm=TRUE)
  upper_bound1 = mean1 + qnorm((1-significance_level/2))*sd1 
  lower_bound1 = mean1 + qnorm(significance_level/2)*sd1
  print(c(paste0("lower bound1 =",lower_bound1),paste0("upper bound1 =",upper_bound1)))
  
  mean2=mean(h,na.rm=TRUE)
  sd2=sd(h,na.rm=TRUE)
  upper_bound2 = mean2 + qnorm((1-significance_level/2))*sd2 
  lower_bound2 = mean2 + qnorm(significance_level/2)*sd2
  print(c(paste0("lower bound2 =",lower_bound2),paste0("upper bound2 =",upper_bound2)))
  
  mean3=mean(d,na.rm=TRUE)
  sd3=sd(d,na.rm=TRUE)
  upper_bound3 = mean3 + qnorm((1-significance_level/2))*sd3 
  lower_bound3 = mean3 + qnorm(significance_level/2)*sd3
  print(c(paste0("lower bound3 =",lower_bound3),paste0("upper bound3 =",upper_bound3)))
  
  mean4=mean(v,na.rm=TRUE)
  sd4=sd(v,na.rm=TRUE)
  upper_bound4 = mean4 + qnorm((1-significance_level/2))*sd4
  lower_bound4 = mean4 + qnorm(significance_level/2)*sd4
  print(c(paste0("lower bound4 =",lower_bound4),paste0("upper bound4 =",upper_bound4)))
  
  mean5=mean(c,na.rm=TRUE)
  sd5=sd(c,na.rm=TRUE)
  upper_bound5 = mean5 + qnorm((1-significance_level/2))*sd5
  lower_bound5 = mean5 + qnorm(significance_level/2)*sd5
  print(c(paste0("lower bound5 =",lower_bound5),paste0("upper bound5 =",upper_bound5)))
  
  mean6=mean(cr,na.rm=TRUE)
  sd6=sd(cr,na.rm=TRUE)
  upper_bound6 = mean5 + qnorm((1-significance_level/2))*sd6
  lower_bound6 = mean5 + qnorm(significance_level/2)*sd6
  print(c(paste0("lower bound6 =",lower_bound6),paste0("upper bound6 =",upper_bound6)))
  
  plot(result1)
  
  h[is.na(h)] = 0
  m[is.na(m)] = 0
  d[is.na(d)] = 0
  v[is.na(v)] = 0
  c[is.na(c)] = 0
  cr[is.na(cr)] = 0
  mean_ooc_condition = FALSE
  homogeneity_ooc_condition = FALSE
  dissimilarity_ooc_condition = FALSE
  variance_ooc_condition =FALSE
  contrast_ooc_condition =FALSE
  correlation_ooc_condition =FALSE
  for(i in 6:506){
    for(j in 6:506){
      
      mean_ooc_condition = m[i,j]>upper_bound1 | m[i,j] < lower_bound1
      homogeneity_ooc_condition = h[i,j]>upper_bound2 | h[i,j] < lower_bound2
      dissimilarity_ooc_condition = d[i,j]>upper_bound3 | d[i,j] < lower_bound3
      variance_ooc_condition = v[i,j]>upper_bound4 | v[i,j] < lower_bound4
      contrast_ooc_condition = c[i,j]>upper_bound5 | c[i,j] < lower_bound5
      correlation_ooc_condition = cr[i,j]>upper_bound6 | cr[i,j] < lower_bound6
      
      if( mean_ooc_condition + homogeneity_ooc_condition + dissimilarity_ooc_condition+ variance_ooc_condition + contrast_ooc_condition+correlation_ooc_condition >=3){
        photo_jpg_defects[i,j,1]=0
      }
      
    }
    
  }
  
  plot_image <- function(photo_jpg, photo_jpg_defects){
  par(mfrow=c(1,2))
    
  plot(0.5:512.5,0.5:512.5,type="n", main="Image without Defects", xlab = "Horizontal", ylab = "Vertical")
  rasterImage(photo_jpg[,,1], 0.5,0.5, 512.5, 512.5,interpolate=FALSE)
  
   plot(0.5:512.5,0.5:512.5,type="n", main="Image with Defects", xlab = "Horizontal", ylab = "Vertical")
  rasterImage(photo_jpg_defects[,,1], 0.5,0.5, 512.5, 512.5,interpolate=FALSE) 
  
  }
  
  plot_image(photo_jpg, photo_jpg_defects)
  
  return(photo_jpg)
  
}

4) Monitoring all images with significance level = 0.002

We applied our detect out of control function to last 10 photos to monitor the defects.

photo_11_defect = detect_out_of_control(photo_11, photo_11_jpg, significance_level = 0.002)
## [1] "lower bound1 =0.588572316275934" "upper bound1 =0.698785130761809"
## [1] "lower bound2 =0.171538940161974" "upper bound2 =0.333733840270057"
## [1] "lower bound3 =2.08149540526894" "upper bound3 =4.78922538455637"
## [1] "lower bound4 =353.960159364943" "upper bound4 =482.36796549822" 
## [1] "lower bound5 =3.0792774827063"  "upper bound5 =35.9584406512737"
## [1] "lower bound6 =19.2519505388264" "upper bound6 =19.7857675951535"

photo_12_defect = detect_out_of_control(photo_12, photo_12_jpg, significance_level = 0.002)
## [1] "lower bound1 =0.093590094100105" "upper bound1 =0.321227308800705"
## [1] "lower bound2 =0.305063623276751" "upper bound2 =0.591865883138603"
## [1] "lower bound3 =0.466058086991518" "upper bound3 =2.81962948002321" 
## [1] "lower bound4 =-35.1420746071956" "upper bound4 =130.085954182892" 
## [1] "lower bound5 =-5.29602620756683" "upper bound5 =15.4353451894378" 
## [1] "lower bound6 =4.81907938271556" "upper bound6 =5.3202395991554"

photo_13_defect = detect_out_of_control(photo_13, photo_13_jpg, significance_level = 0.002)
## [1] "lower bound1 =0.40590067324843"  "upper bound1 =0.585344360702264"
## [1] "lower bound2 =0.164185622099186" "upper bound2 =0.263545491883759"
## [1] "lower bound3 =3.2341383140951" "upper bound3 =4.7770702274994"
## [1] "lower bound4 =168.651752127172" "upper bound4 =357.273655955551"
## [1] "lower bound5 =15.7757774433568" "upper bound5 =34.5125939295997"
## [1] "lower bound6 =24.9116846034169" "upper bound6 =25.3766867695397"

photo_14_defect = detect_out_of_control(photo_14, photo_14_jpg, significance_level = 0.002)
## [1] "lower bound1 =0.510487168944083" "upper bound1 =0.577753533793005"
## [1] "lower bound2 =0.171802834710958" "upper bound2 =0.291740627530944"
## [1] "lower bound3 =2.82627114592802" "upper bound3 =4.61256778777483"
## [1] "lower bound4 =263.390014756233" "upper bound4 =336.294579138816"
## [1] "lower bound5 =11.7892561666142" "upper bound5 =32.5945257406456"
## [1] "lower bound6 =21.9883747468812" "upper bound6 =22.3954071603786"

photo_15_defect = detect_out_of_control(photo_15, photo_15_jpg, significance_level = 0.002)
## [1] "lower bound1 =0.388750580169155" "upper bound1 =0.559485314518742"
## [1] "lower bound2 =0.0771165022873242" "upper bound2 =0.559106991237954" 
## [1] "lower bound3 =0.525939122511053" "upper bound3 =4.71123355692347" 
## [1] "lower bound4 =149.330710599736" "upper bound4 =308.620140458673"
## [1] "lower bound5 =-4.31750839763859" "upper bound5 =27.435141511576"  
## [1] "lower bound6 =11.3562557696241" "upper bound6 =11.7613773443133"

photo_16_defect = detect_out_of_control(photo_16, photo_16_jpg, significance_level = 0.002)
## [1] "lower bound1 =0.660679447372228" "upper bound1 =0.895381811051009"
## [1] "lower bound2 =0.360362087468391" "upper bound2 =0.590313714904333"
## [1] "lower bound3 =0.772423461528733" "upper bound3 =2.07341733621462" 
## [1] "lower bound4 =454.529689655679" "upper bound4 =720.244997540395"
## [1] "lower bound5 =-0.320260718448338" "upper bound5 =7.31536048585567"  
## [1] "lower bound6 =3.32216926119882" "upper bound6 =3.67293050620851"

photo_17_defect = detect_out_of_control(photo_17, photo_17_jpg, significance_level = 0.002)
## [1] "lower bound1 =0.231484877422973" "upper bound1 =0.418793930663333"
## [1] "lower bound2 =0.183445713855304" "upper bound2 =0.355435249103115"
## [1] "lower bound3 =2.28612612386368" "upper bound3 =4.57165880215206"
## [1] "lower bound4 =52.2431977352716" "upper bound4 =207.571803994725"
## [1] "lower bound5 =7.43144060156032" "upper bound5 =33.417195514637" 
## [1] "lower bound6 =20.2650182595898" "upper bound6 =20.5836178566076"

photo_18_defect = detect_out_of_control(photo_18, photo_18_jpg, significance_level = 0.002)
## [1] "lower bound1 =0.46915726883755"  "upper bound1 =0.624853472850562"
## [1] "lower bound2 =0.1880422194089"   "upper bound2 =0.315292923710664"
## [1] "lower bound3 =2.60324729545347" "upper bound3 =4.19986408509123"
## [1] "lower bound4 =227.41465129189"  "upper bound4 =399.535482449543"
## [1] "lower bound5 =10.3303501450681" "upper bound5 =27.1519725318921"
## [1] "lower bound6 =18.5714184917142" "upper bound6 =18.9109041852459"

photo_19_defect = detect_out_of_control(photo_19, photo_19_jpg, significance_level = 0.002)
## [1] "lower bound1 =0.368190491000542" "upper bound1 =0.595625305866468"
## [1] "lower bound2 =0.227061910460523" "upper bound2 =0.383421302353761"
## [1] "lower bound3 =1.9473125374308"  "upper bound3 =3.39518852655693"
## [1] "lower bound4 =134.223514441901" "upper bound4 =354.589702508436"
## [1] "lower bound5 =5.65264704874379" "upper bound5 =17.6917872397041"
## [1] "lower bound6 =11.5042949753897" "upper bound6 =11.8401393130582"

photo_20_defect = detect_out_of_control(photo_20, photo_20_jpg, significance_level = 0.001)
## [1] "lower bound1 =0.450402030633373" "upper bound1 =0.584143449686978"
## [1] "lower bound2 =0.165753414493562" "upper bound2 =0.282100158940134"
## [1] "lower bound3 =2.94701563959096" "upper bound3 =4.63942622049054"
## [1] "lower bound4 =205.955211607886" "upper bound4 =345.704295479048"
## [1] "lower bound5 =12.9552956737276" "upper bound5 =32.1655404061787"
## [1] "lower bound6 =22.4086442426865" "upper bound6 =22.7121918372198"